<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 前台父控制器
 * 
 * 前台所有控制器都需要继承这个类，不包含验证
 */

class ST_Controller extends CI_Controller{
    
    //系统参数
    public $system;
    
    public function __construct() {
        parent::__construct();
        /** 开启session */
        $this->load->library('session');
        /** 开启cookie */
        $this->load->helper('cookie');
        /** 检查站点当前状态 */
        $this->load->model('system_mdl');
        $this->system = $this->system_mdl->distribution();
        if($this->system->status == 0){
            show_error('当前站点未开启');
        }
		//print_r($_SERVER);exit;
    }
    
}


/**
 * 后台父控制器
 * 
 * 后台所有控制器都需要继承这个类， 包含验证
 */
class Auth_Controller extends CI_Controller{
    public $luser;  //登陆数据
    
    public function __construct() {
        parent::__construct();
        
        /** 加载后台控制器公共库 */
        $this->load->library('session');
        /** 检查登陆 */
        $user = $this->session->userdata('username');
        if(!empty($user)){
            $this->luser = $user;
        }else{
            redirect('admini/login/index');
        }
        /** 加载后台控制器公共模型 */
        $this->load->model('logs_mdl','logs');
    }
    
    /**
     * 视图加载
     */
    protected function _view($str,$data=array()){
        $this->load->view('admin/layout/head.php',$data);
        $this->load->view($str);
        $this->load->view('admin/layout/bottom');
    }

    /**
     * 系统发送错误提示管理员
     */
    public function email_admin($message,$title = '程序运行错误提示消息'){
        $this->load->library('email');

        $config['protocol'] = 'smtp';
        $config['smtp_host'] = 'smtp.yeah.net';
        $config['smtp_user'] = 'dickfan@yeah.net';
        $config['smtp_pass'] = 'vxktonsmeebqhomr';
        $config['smtp_port'] = '25';
        $config['charset'] = 'utf-8';
        $config['wordwrap'] = TRUE;
        $this->email->initialize($config);

        $this->email->from('dickfan@yeah.net', '买得网自动错误提醒');
        //发送到管理员邮箱
        $this->email->to('dickfan@yeah.net');

        //设置主题
        $this->email->subject($title);
        //设置正文部分
        $mess = '时间：'.date('Y-m-d H:i:s')."\n";
        $mess .= '-----------------------  '."\n";
        $mess .= $message;
        $this->email->message($mess);
        $this->email->send();
    }
}

class SHOPS_Controller extends ST_Controller{

    /**
     * 商户后台数据控制
     * session格式
     * array(
     *     'store' => [...],
     *     'suser' => [...]
     * )
     */
    static public $store;   //店铺信息
    static public $suser;   //用户信息

    public function __construct() {
        parent::__construct();

        self::$store = $this->session->store;
        self::$suser = $this->session->suser;

        if(empty(self::$suser)){
            $this->session->unset_userdata('store');
            redirect(site_url('shops/login/index'));
        }
    }

    /**
     * 视图加载
     */
    protected function _view($str,$data=array()){
        $data['store'] = self::$store;
        $data['suser'] = self::$suser;
        $this->load->view('shops/layout/head.php',$data);
        $this->load->view('shops/'.$str);
        $this->load->view('shops/layout/bottom');
    }
}
